<?php
/**
 * Created by PhpStorm.
 * User: HP
 * Date: 2018/5/8
 * Time: 16:40
 */

namespace app\user;

use app\common\Common;
use app\data\UserData;
use think\Db;
use wechat\WeChat;

class User extends Base
{

    /**
     * 用户名字长度限制
     * @var array
     */
    private $userName = [2, 10];


    /**
     * 排序方法
     * @var array
     */
    protected $sortType = ['desc', 'asc'];

    /**
     * 原始会员排序
     * @var array
     */
    private $sortOriginal = ['create_time'];


    /**
     * 原始会员排序
     * @var array
     */
    private $sortFriend = ['create_time'];

    //开户人姓名长度限制
    private $bank_user_name = [1, 20];
    //开户银行姓名长度
    private $bank_name = [1, 200];

    //修改用户信息的选项名称
    private $table_name = ['user_name', 'user_head_img', 'user_sex'];

    private $sortWithdraw = ['create_time'];//提现审核排序

    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
    }


    private function checkOriginal($user_id, $user_name, $card_number, $img_positive, $img_back, $img_signature, $integral)
    {
        if (Common::checkTableFields('drh_user_info', 'id', $user_id) == $this->f_code) {
            return ['code' => $this->f_code, 'msg' => '用户不存在'];
        }

        if (!Common::validateIdCard($card_number)) {
            return ['code' => $this->f_code, 'msg' => '身份证号码错误'];
        }
        if (!is_numeric($integral) || $integral < 0) {
            return ['code' => $this->f_code, 'msg' => '输入投股数错误'];
        }
        $len = mb_strlen($user_name);
        if ($len < $this->userName[0] || $len > $this->userName[1]) {
            return ['code' => $this->f_code, 'msg' => '请输入正确的姓名'];
        }
        $bool_positive = preg_match('/.*(\.png|\.jpg|\.jpeg|\.gif)$/', $img_positive);
        $bool_back = preg_match('/.*(\.png|\.jpg|\.jpeg|\.gif)$/', $img_back);
        $bool_signature = preg_match('/.*(\.png|\.jpg|\.jpeg|\.gif)$/', $img_signature);
        if ($bool_positive != 1 && $bool_back != 1 && $bool_signature != 1) {
            return ['code' => $this->f_code, 'msg' => '图片格式错误'];
        }
        if (mb_strlen($img_positive) > 200 || mb_strlen($img_back) > 200) {
            return ['code' => $this->f_code, 'msg' => '图片长度过长'];
        }
        return ['code' => $this->t_code];
    }

    /**
     * 申请创始会员
     * @param $user_id
     * @param $user_name
     * @param $card_number
     * @param $img_positive
     * @param $img_back
     * @param $img_signature
     * @param $integral
     * @return \think\response\Json
     */
    public function addOriginalMember($user_id, $user_name, $card_number, $img_positive, $img_back, $img_signature, $integral)
    {

        $res = $this->checkOriginal($user_id, $user_name, $card_number, $img_positive,
            $img_back, $img_signature, $integral);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        if (Common::checkTableFields('drh_original_member', 'user_id', $user_id) == $this->t_code) {
            return json(['code' => $this->f_code, 'msg' => '请勿重复申请']);
        }
        $user = new UserData();
        $time = time();
        $res = $user->addOriginalMember($user_id, $user_name, $card_number, $time, $img_positive, $img_back, $img_signature, $integral);
        return is_numeric($res) ? json([
            'code' => $this->t_code, 'msg' => '申请成功,请等待人工审核'
        ]) : json([
            'code' => $this->f_code, 'msg' => '申请失败请稍后重试'
        ]);
    }

    /**
     * 修改创始会员提交信息
     * @param $user_id
     * @param $user_name
     * @param $card_number
     * @param $img_positive
     * @param $img_back
     * @param $img_signature
     * @param $integral
     * @return \think\response\Json
     */
    public function editOriginalMember($user_id, $user_name, $card_number, $img_positive, $img_back, $img_signature, $integral)
    {
        $res = $this->checkOriginal($user_id, $user_name, $card_number, $img_positive,
            $img_back, $img_signature, $integral);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        if (Common::checkTableFields('drh_original_member', 'user_id', $user_id) == $this->f_code) {
            return json(['code' => $this->f_code, 'msg' => '不存在该创始会员']);
        }
        $user = new UserData();
        $time = time();
        $res = $user->editOriginalMember($user_id, $user_name, $card_number, $time, $img_positive, $img_back, $img_signature, $integral);
        return is_numeric($res) ? json([
            'code' => $this->t_code, 'msg' => '申请成功,请等待人工审核'
        ]) : json([
            'code' => $this->f_code, 'msg' => '申请失败请稍后重试'
        ]);
    }

    /**
     * 显示
     * @param int $page
     * @param int $pre
     * @param array $sort
     * @param int $status
     * @param int $recover
     * @return \think\response\Json
     */
    public function showOriginalMember($page = 1, $pre = 15, $sort = ['create_time' => 'desc'], $status = 0, $recover = 0)
    {
        //校验页码数和页码条数已经排序
        $res = Common::checkShow($page, $pre, $this->sortOriginal, $this->sortType, $sort);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        $where = 'recover_status = ' . $recover;
        switch ($status) {
            case 1:
                $where .= ' and verify_status=0';
                break;
            case 2:
                $where .= ' and verify_status=1';
                break;
            case 3:
                $where .= ' and verify_status=2';
                break;
            default:
                break;
        }
        $user = new UserData();
        return json([
            'code' => $this->t_code,
            'msg' => 'OK',
            'data' => [
                'list' => $user->showOriginalMember($page, $pre, $sort, $where),
                'total' => $user->total($where, 'drh_original_member')
            ]
        ]);
    }

    /**
     * 后台审核
     * @param $id
     * @param $user_id
     * @param $status
     * @param string $msg
     * @param string $additional_mgs
     * @return \think\response\Json
     */
    public function reviewOperating($id, $user_id, $status, $msg = 'OK', $additional_mgs = '审核通过')
    {
        $id = Common::getTableValue('drh_original_member', 'id', ['id', 'user_id'], [$id, $user_id]);
        if (empty($id)) {
            return json(['code' => $this->f_code, 'msg' => '不存在申请']);
        }
        if ($status != 1 && $status != 2) {
            return json(['code' => $this->f_code, 'msg' => '状态错误']);
        }
        $user = new UserData();
        $time = time();
        Db::startTrans();
        $res = $user->reviewOperating($id, $user_id, $status);
        $return = Common::addMsg($user_id, 0, $time, $msg, $additional_mgs);
        if ($res != 1 || !is_numeric($return)) {
            Db::rollback();
            return json(['code' => $this->f_code, 'msg' => '数据错误']);
        }
        Db::commit();
        return json(['code' => $this->t_code, 'msg' => '审核成功']);
    }

//    /**
//     *计算某个经纬度的周围某段距离的正方形的四个点
//     *
//     * @param lng float 经度
//     * @param lat float 纬度
//     * @param distance float 该点所在圆的半径，该圆与此正方形内切，默认值为0.5千米
//     * @return array 正方形的四个点的经纬度坐标
//     */
//    public function returnSquarePoint($lng, $lat, $distance = 0.5)
//    {
//        define('EARTH_RADIUS', 6371);//地球半径，平均半径为6371km
//        $d_lng = 2 * asin(sin($distance / (2 * EARTH_RADIUS)) / cos(deg2rad($lat)));
//        $d_lng = rad2deg($d_lng);
//
//        $d_lat = $distance / EARTH_RADIUS;
//        $d_lat = rad2deg($d_lat);
//
//        return array(
//            'left-top' => array('lat' => $lat + $d_lat, 'lng' => $lng - $d_lng),
//            'right-top' => array('lat' => $lat + $d_lat, 'lng' => $lng + $d_lng),
//            'left-bottom' => array('lat' => $lat - $d_lat, 'lng' => $lng - $d_lng),
//            'right-bottom' => array('lat' => $lat - $d_lat, 'lng' => $lng + $d_lng)
//        );
//    }
//
//    public function getDistance($lng1, $lat1, $lng2, $lat2)
//    {//根据经纬度计算距离 单位为公里
////将角度转为狐度
//        $radLat1 = deg2rad($lat1);
//        $radLat2 = deg2rad($lat2);
//        $radLng1 = deg2rad($lng1);
//        $radLng2 = deg2rad($lng2);
//        $a = $radLat1 - $radLat2;//两纬度之差,纬度<90
//        $b = $radLng1 - $radLng2;//两经度之差纬度<180//6378.137
//        $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))) * 6371;
//        return $s;
//    }
//
//    /**
//     * @param lng float 经度
//     * @param lat float 纬度
//     */
//    function test($lng = '104.2407917976', $lat = '30.5668523045')
//    {
//        $squares = $this->returnSquarePoint($lng, $lat, 5);
//        dump($squares);
//        dump($this->getDistances());
//        exit;
////        30.5668523045  104.2407917976   30.611818384796  104.18856859119
//        $condition = "location_x<>0
//        and
//        location_x>{$squares['right-bottom']['lat']}
//        and
//        location_x<{$squares['left-top']['lat']}
//        and
//        location_y>{$squares['left-top']['lng']}
//        and
//        location_y<{$squares['right-bottom']['lng']}";
//    }
//
//    public function getDistances($lat1='30.5668523045', $lng1='104.2407917976', $lat2='30.611818384796', $lng2='104.18856859119')
//    {
//        $earthRadius = 6367000; //approximate radius of earth in meters
//        $lat1 = ($lat1 * pi()) / 180;
//        $lng1 = ($lng1 * pi()) / 180;
//        $lat2 = ($lat2 * pi()) / 180;
//        $lng2 = ($lng2 * pi()) / 180;
//        $calcLongitude = $lng2 - $lng1;
//        $calcLatitude = $lat2 - $lat1;
//        $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
//        $stepTwo = 2 * asin(min(1, sqrt($stepOne)));
//        $calculatedDistance = $earthRadius * $stepTwo;
//        return round($calculatedDistance);
//    }

    /**
     * 添加好友
     * @param $user_id
     * @param $friend_id
     * @param $type
     * @return \think\response\Json
     */
    public function addFriend($user_id, $friend_id, $type)
    {
        if (Common::checkTableFields('drh_user_info', 'id', $user_id) == $this->f_code) {
            return json(['code' => $this->f_code, 'msg' => '用户不存在']);
        }
        if (Common::checkTableFields('drh_user_info', 'id', $friend_id) == $this->f_code) {
            return json(['code' => $this->f_code, 'msg' => '用户不存在']);
        }
        if ($user_id == $friend_id) {
            return json(['code' => $this->f_code, 'msg' => '不能添加自己为好友']);
        }
        $id = Common::getTableValue('drh_user_friend', 'friend_id', ['friend_id', 'user_id'], [$friend_id, $user_id]);
        if (!empty($id)) {//存在好友
            return json(['code' => $this->f_code, 'msg' => '好友以存在 请勿重复添加']);
        }
        $id = Common::getTableValue('drh_user_friend', 'friend_id', ['friend_id'], [$friend_id]);
        if (!empty($id)) {//存在好友
            return json(['code' => $this->f_code, 'msg' => '该好友已有上级']);
        }

        if ($type != 0 && $type != 1 && $type != 2) {
            return json(['code' => $this->f_code, 'msg' => '好友来源错误']);
        }
        $user = new UserData();
        $time = time();
        $id = $user->addFriend($user_id, $friend_id, $time, $type);
        if ($id) {
            return json(['code' => $this->t_code, 'msg' => '添加成功']);
        } else {
            return json(['code' => $this->f_code, 'msg' => '添加失败 请稍后重试!']);
        }
    }

    /**
     * 好友显示
     * @param $user_id
     * @param int $page
     * @param int $pre
     * @param array $sort
     * @return \think\response\Json
     */
    public function showFriend($user_id, $page = 1, $pre = 5, $sort = ['create_time' => 'desc'])
    {
        if (Common::checkTableFields('drh_user_info', 'id', $user_id) == $this->f_code) {
            return json(['code' => $this->f_code, 'msg' => '用户不存在']);
        }

        //校验页码数和页码条数已经排序
        $res = Common::checkShow($page, $pre, $this->sortFriend, $this->sortType, $sort);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }

        $user = new UserData();
        $data = $user->showFriend($user_id, $page, $pre, $sort);
        return json([
            'code' => $this->t_code,
            'msg' => 'OK',
            'data' => [
                'list' => $data,
                'total' => $user->total("user_id={$user_id}", 'drh_user_friend')
            ]
        ]);
    }


    /**
     * 用户信息显示
     * @param $user_id
     * @return \think\response\Json
     */
    public function showUserInfo($user_id)
    {
        if (Common::checkTableFields('drh_user_info', 'id', $user_id) == $this->f_code) {
            return json(['code' => $this->f_code, 'msg' => '用户不存在']);
        }
        $user = new UserData();
        return json(['code' => $this->t_code, 'msg' => 'OK', 'data' => $user->getUserInfo($user_id)]);
    }

    /**
     * 添加用户信息
     * @param $user_id
     * @param $name
     * @param $value
     * @return \think\response\Json
     */
    public function addUserInfo($user_id, $name, $value)
    {
        if (Common::checkTableFields('drh_user_info', 'id', $user_id) == $this->f_code) {
            return json(['code' => $this->f_code, 'msg' => '用户不存在']);
        }
        if (!in_array($name, $this->table_name)) {
            return json(['code' => $this->f_code, 'msg' => '修改选项错误!']);
        }
        $user = new UserData();
        if ($user->addUserInfo($user_id, $name, $value)) {
            return json(['code' => $this->t_code, 'msg' => '修改成功']);
        } else {
            return json(['code' => $this->f_code, 'msg' => '修改失败请稍后在试']);
        }
    }


    private function checkBankCard($user_id, $user_name, $bank_name)
    {
        if (Common::checkTableFields('drh_user_info', 'id', $user_id) == $this->f_code) {
            return ['code' => $this->f_code, 'msg' => '不存在该用户'];
        }


        //校验字符串长度
        $check = [
            '开户人姓名' => ["{$this->bank_user_name[0]},{$this->bank_user_name[1]}}", $user_name],
            '开户银行名称' => ["{$this->bank_name[0]},{$this->bank_name[1]}}", $bank_name],
        ];
        $res = Common::checkLength($check);
        if ($res['code'] == $this->f_code) {
            return $res;
        }
        return ['code' => $this->t_code];
    }

    /**
     * 添加银行卡号    对公账户 和名称
     * @param $user_id
     * @param $user_name |  开户人姓名    对公账户
     * @param $bank_name |  开户人姓名    对公账户名称(什么什么名称)
     * @param $card |        开户卡号      对公账户(卡号)
     * @param $bank |        开户行      对公账户(卡号)
     * @return array|\think\response\Json
     */
    public function addBankCard($user_id, $user_name, $bank_name, $card, $bank = '')
    {
        $res = $this->checkBankCard($user_id, $user_name, $bank_name);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        $id = Common::getTableValue('drh_bank_card', 'user_id', ['user_id', 'type_id'], [$user_id, 1]);
        if (!empty($id)) {
            return json(['code' => $this->f_code, 'msg' => '每个个体只能添加一张银行卡']);
        }
        if ($user_name == '对公账户') {
            $res['msg'] = $bank;
        } else {
            $res = Common::bankInfo($card);
            if ($res['code'] == $this->f_code) {
                return json($res);
            }
        }
        $user = new UserData();
        return json($user->addBankCard($user_id, $user_name, $bank_name, time(), $res['msg'], $card));
    }

    /**
     * 修改银行卡号
     * @param $user_id
     * @param $user_name
     * @param $bank_name
     * @param $card
     * @return \think\response\Json
     */
    public function editBankCard($user_id, $user_name, $bank_name, $card)
    {
        $res = $this->checkBankCard($user_id, $user_name, $bank_name);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        $res = Common::bankInfo($card);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        $user = new UserData();
        return json($user->editBankCard($user_id, $user_name, $bank_name, $res['msg'], $card));
    }

    /**
     * 展示银行卡信息
     * @param $user_id
     * @return array|\think\response\Json
     */
    public function showBankCardInfo($user_id)
    {

        if (Common::checkTableFields('drh_bank_card', 'user_id', $user_id) == $this->f_code) {
            return json(['code' => $this->f_code, 'msg' => '请添加银行卡']);
        }
        $user = new UserData();
        $res = $user->showBankCardInfo($user_id);
        $sub_cart = substr($res['card_number'], -4);
        $length = mb_strlen($res['card_number']);
        $fill_array = array_fill(0, $length - 4, "*");
        $fill_str = implode($fill_array);
        $res['card_number'] = $fill_str . $sub_cart;
        return json([
            'code' => $this->t_code,
            'msg' => 'OK',
            'data' => $res
        ]);
    }

    /**
     * 积分提现申请
     * @param $user_id
     * @param $integral
     * @param $img
     * @return \think\response\Json
     */
    public function integralWithdraw($user_id, $integral, $img)
    {
        $user_info = Common::getTableValue('drh_user_info', ['user_integral', 'user_money'], ['id'], [$user_id]);
        if (empty($user_info)) {
            return json(['code' => $this->f_code, 'msg' => '不存在该用户']);
        }
        if ($integral > $user_info['user_integral']) {
            return json(['code' => $this->f_code, 'msg' => '积分不足']);
        }
        //获取提现利息
        $withdraw_interest = Common::getTableValue('drh_admin_config', 'value', ['name'], ['withdraw_interest']);
        $time = time();
        $integral_ratio = Common::getTableValue('drh_admin_config', 'value', ['name'], ['integral_ratio']);
        $order_num = Common::getOrderNumber('w', 10, $time);//单号
        $interest = ($integral * ($withdraw_interest / 100)) / $integral_ratio * 100;//分
        $money = $integral / $integral_ratio * 100;//钱
        $user = new UserData();
        Db::startTrans();
        $bool1 = $user->integralWithdraw($user_id, $order_num, $integral, $interest, $money, $time, $img);
        $bool2 = $user->changIntegralMoney($user_id, $user_info['user_integral'] - $integral, $user_info['user_money']);
        if (!$bool1 || !$bool2) {
            Db::rollback();
            return json(['code' => $this->f_code, 'msg' => '系统繁忙请稍后在试']);
        }
        Db::commit();
        return json(['code' => $this->t_code, 'msg' => '质押成功,请等待审核']);
    }

    /**
     * 显示审核列表
     * @param int $page
     * @param int $pre
     * @param array $sort
     * @param int $type
     * @return \think\response\Json
     */
    public function showReviewWithdraw($page = 1, $pre = 15, $sort = ['create_time' => 'desc'], $type = 0)
    {
        //校验页码数和页码条数已经排序
        $res = Common::checkShow($page, $pre, $this->sortWithdraw, $this->sortType, $sort);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        $where = $type == 0 ? "" : "verify_status = {$type}-1";
        $user = new UserData();
        return json([
            'code' => $this->t_code,
            'msg' => 'OK',
            'data' => [
                'list' => $user->showReviewWithdraw($page, $pre, $sort, $where),
                'total' => $user->total($where, 'drh_integral_withdraw')
            ]
        ]);
    }

    /**
     * 提现审核
     * @param $user_id
     * @param $status
     * @param $order_num
     * @param string $msg
     * @return \think\response\Json
     */
    public function reviewWithdraw($user_id, $status, $order_num, $msg = '审核成功！')
    {
        $user_integral = Common::getTableValue('drh_integral_withdraw', ['integral', 'money', 'interest'], ['order_num', 'user_id'], [$order_num, $user_id]);
        if (empty($user_integral)) {
            return json(['code' => $this->f_code, 'msg' => '不存在该用户']);
        }

        $user_info = Common::getTableValue('drh_user_info', ['user_integral', 'user_money'], ['id'], [$user_id]);
        if (empty($user_info)) {
            return json(['code' => $this->f_code, 'msg' => '不存在该用户']);
        }
        $user = new UserData();
        $time = time();
        $additional_mgs = $msg;
        Db::startTrans();
        switch ($status) {
            case 1://审核通过
                $num1 = $user->addReviewWithdrawFlow($user_integral, $time, $user_id, $order_num);
                $msg = '积分提现成功:' . $order_num;
                break;
            case 2://审核失败
                $num1 = $user->changIntegralMoney($user_id, $user_info['user_integral'] + $user_integral['integral'], $user_info['user_money']);
                $msg = '审核失败:' . $order_num;
                break;
            default:
                return json(['code' => $this->f_code, 'msg' => '审核状态错误']);
        }
        if (!$user->reviewWithdraw($user_id, $status, $order_num, $msg, $time)) {
            Db::rollback();
            return json(['code' => $this->f_code, 'msg' => '系统繁忙请稍后在试']);
        }
        $num2 = Common::addMsg($user_id, 0, $time, $msg, $additional_mgs);
        if (!$num2 || !$num1) {
            Db::rollback();
            return json(['code' => $this->f_code, 'msg' => '系统繁忙请稍后在试']);
        }
        Db::commit();
        return json(['code' => $this->t_code, 'msg' => '审核成功!']);
    }

    /**
     * 消息显示
     * @param $user_id
     * @param int $page
     * @param int $pre
     * @param array $sort
     * @param int $status
     * @return array|\think\response\Json
     */
    public function showMsg($user_id, $page = 1, $pre = 15, $sort = ['create_time' => 'desc'], $status = 2)
    {
        if (Common::checkTableFields('drh_user_info', 'id', $user_id) == $this->f_code) {
            return json(['code' => $this->f_code, 'msg' => '不存在该用户']);
        }
        //校验页码数和页码条数已经排序
        $res = Common::checkShow($page, $pre, $this->sortOriginal, $this->sortType, $sort);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        $user = new UserData();
        $where = "user_id = {$user_id} and status=1";
        if (in_array($status, [0, 1])) {
            $where = $status == 1 ? $where .= " and end_time > 0" : $where .= " and end_time = 0";
        }
        $data = $user->showMsg($where, $page, $pre, $sort);
        return json([
            'code' => $this->t_code,
            'msg' => 'OK',
            'data' => [
                'list' => $data,
                'total' => $user->total("user_id={$user_id} and status=1", 'drh_msg'),
                'unread_total' => $user->total("user_id={$user_id} and end_time=0 and status=1", 'drh_msg'),
            ]
        ]);
    }

    public function delUserMsg($ids, $user_id)
    {
        if (!is_array($ids)) {
            return json(['code' => $this->f_code, 'msg' => '参数传递错误！']);
        }
        if (Common::checkTableFields('drh_user_info', 'id', $user_id) == $this->f_code) {
            return json(['code' => $this->f_code, 'msg' => '不存在该用户']);
        }
        $where = implode(",", $ids);
        $user = new UserData();
        $time = time();
        $res = $user->delUserMsg($where, $user_id, $time);
        if ($res) {
            return json(['code' => $this->t_code, 'msg' => '删除成功']);
        } else {
            return json(['code' => $this->f_code, 'msg' => '删除失败请联系管理员']);
        }
    }

    public function readMsg($ids, $user_id)
    {
        if (!is_array($ids)) {
            return json(['code' => $this->f_code, 'msg' => '参数传递错误！']);
        }
        if (Common::checkTableFields('drh_user_info', 'id', $user_id) == $this->f_code) {
            return json(['code' => $this->f_code, 'msg' => '不存在该用户']);
        }
        $where = implode(",", $ids);
        $user = new UserData();
        $time = time();
        $res = $user->readUserMsg($where, $user_id, $time);
        if ($res) {
            return json(['code' => $this->t_code, 'msg' => '调用成功']);
        } else {
            return json(['code' => $this->f_code, 'msg' => '调用失败，请联系管理员']);
        }
    }


    public function showIntegralRecord($user_id, $type = 0, $page = 1, $pre = 15)
    {
        $sort = ['create_time' => 'desc'];
        if (Common::checkTableFields('drh_user_info', 'id', $user_id) == $this->f_code) {
            return ['code' => $this->f_code, 'msg' => '不存在该用户'];
        }
        //校验页码数和页码条数已经排序
        $res = Common::checkShow($page, $pre, $this->sortOriginal, $this->sortType, $sort);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        $where = "a.user_id={$user_id}";
        if ($type != 0 && is_numeric($type) && ($type > 0 && $type < 10)) {
            $where .= " and type_status={$type}";
        }
        $user = new UserData();
        return json([
            'code' => $this->t_code,
            'msg' => 'OK',
            'data' => [
                'list' => $user->showIntegralRecord($where, $page, $pre, $sort),
                'total' => $user->total('user_id=' . $user_id, 'drh_user_flow')
            ]
        ]);
    }

    /**
     * 微信绑定手机号
     * @param $data
     * @param $vi
     * @param $sessionKey
     * @param $user_id
     * @return array|\think\response\Json
     */
    public function WXUserTelBind($data, $vi, $sessionKey, $user_id)
    {
        if (Common::checkTableFields('drh_user_info', 'id', $user_id) == $this->f_code) {
            return json(['code' => $this->f_code, 'msg' => '不存在该用户']);
        }
        $WeChat = new WeChat();
        $res = $WeChat->decrypt($sessionKey, $data, $vi);
        $res = json_decode($res, true);
        if (empty($res['phoneNumber'])) {
            return json(['code' => $this->f_code, 'msg' => '数据错误', 'data' => $res]);
        }
        $tel = $res['phoneNumber'];
        $user = new UserData();
        $data = $user->WXUserTelBind($user_id, $tel);
        if (!$data) {
            return json(['code' => $this->f_code, 'msg' => '数据错误请稍后再试']);
        }
        return json([
            'code' => $this->t_code,
            'msg' => 'OK',
            'data' => '手机号绑定成功!'
        ]);
    }


///////////////////////////////////////////////-----------------------新增创始会员功能-----------------------////////////////


}